package com.baiye.template.dao;

import com.baiye.template.pojo.ExceptionLog;
import com.baiye.template.pojo.OperationLog;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/29 17:02
 */
@Repository
public interface LogDao {
    @Insert("insert into oper_log(module,type,`desc`,req_param,resp_param,user_id,user_name,method,url,ip,visit_time,version,execution_time) values(#{module},#{type},#{desc},#{reqParam},#{respParam},#{userId},#{userName},#{method},#{url},#{ip},#{visitTime},#{version},#{executionTime})")
    int saveOperLog(OperationLog operlog);
    @Insert("insert into exception_log (name,message,req_param,user_id,user_name,method,visit_time,version) values(#{name},#{message},#{reqParam},#{userId},#{userName},#{method},#{visitTime},#{version})")
    int saveExceptionLog(ExceptionLog excepLog);
//    select * from user where uid >=( select uid from user order by uid limit 10000,1 ) limit 10;     limit优化
    //locate (#{log.userName} , user_name) > 0       like优化
    @Select({"<script>" +
            "select * from oper_log " +
            " <where> " +
//            "<if test='start!=null and size !=null'>id >=( select id from oper_log order by id limit #{start},1 )</if>"+
            " <if test='log.id != null'>AND id=#{log.id}</if> " +
            " <if test='log.userId != null'> AND user_id=#{log.userId}</if> " +
            " <if test='log.userName != null'> AND locate (#{log.userName} , user_name) > 0</if> " +
            " <if test='log.method != null'> AND method=#{log.method}</if> " +
            " <if test='log.ip != null'> AND ip=#{log.ip}</if> " +
            " <if test='log.url != null'> AND url=#{log.url}</if> " +
            " </where> " +
            //pagehelp自动添加limit
           // "<if test='start!=null and size !=null'> limit #{size}</if>"+
            "</script>"})
    List<OperationLog> getOperLog( @Param("log")OperationLog log, @Param("start")Integer start, @Param("size")Integer size);
    @Select({"<script>" +
            "select * from exception_log " +
            " <where> " +
//            "<if test='start!=null and size !=null'>id >=( select id from excption_log order by id limit #{start},1 )</if>"+
            " <if test='log.id != null'>AND id=#{log.id}</if> " +
            " <if test='log.name != null'>AND name=#{log.name}</if> " +
            " <if test='log.userId != null'> AND user_id=#{log.userId}</if> " +
            " <if test='log.userName != null'> AND locate (#{log.userName} , user_name) > 0</if> " +
            " <if test='log.method != null'> AND method=#{log.method}</if> " +
            " <if test='log.ip != null'> AND ip=#{log.ip}</if> " +
            " <if test='log.url != null'> AND url=#{log.url}</if> " +
            " </where> " +
            //pagehelp自动添加limit
            // "<if test='start!=null and size !=null'> limit #{size}</if>"+
            "</script>"})
    List<ExceptionLog> getExceptionLog(@Param("log")ExceptionLog log, @Param("start")Integer start, @Param("size")Integer size);

    @Delete("<script>" +
            "delete from ${tableName} " +
            "where id in"+
            "<foreach item='item' index='index' collection='idList' open='(' separator=',' close=')'>" +
            "#{item}"+
            "</foreach>"+
            "</script>")
    int deleteLog(@Param("idList")List idList, @Param("tableName") String tableName);
}
